package com.bianguo.android.functiondemo.view.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

import com.bianguo.android.functiondemo.R;
import com.bianguo.android.functiondemo.adapter.BaseRecyclerAdapter;

public class SlideRecyclerView extends RecyclerView {

    //1表示有item被打开 0表示没有item打开
    private int state;
    private Scroller mScroller;
    private int lastX, lastY;
    private int deleteViewWidth;
    private View itemView;
    private BaseRecyclerAdapter.BaseViewHolder viewHolder;

    public SlideRecyclerView(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public SlideRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public SlideRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context, attrs);
    }

    private void initView(@NonNull Context mContext, @Nullable AttributeSet attrs) {
        mScroller = new Scroller(mContext);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        int x = (int) e.getX();
        int y = (int) e.getY();
        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (0 == state) {
                    //根据手指按下的位置获取当前的item
                    itemView = findChildViewUnder(x, y);
                    //根据当前的Item获取该Item对应的ViewHolder
                    viewHolder = (BaseRecyclerAdapter.BaseViewHolder) getChildViewHolder(itemView);
                    ViewGroup viewGroup = (ViewGroup) viewHolder.itemView;
                    View childView = viewGroup.getChildAt(1);
                    int id = childView.getId();
                    if (id == R.id.item_delete_layout) {
                        deleteViewWidth = childView.getWidth();
                    }
                } else if (1 == state) {
                    View itemView = findChildViewUnder(x, y);
                    BaseRecyclerAdapter.BaseViewHolder viewHolder = (BaseRecyclerAdapter.BaseViewHolder) getChildViewHolder(itemView);
                    if (this.viewHolder.equals(viewHolder)) {
                        break;
                    } else {
                        mScroller.startScroll(viewHolder.itemView.getScrollX(), 0, -deleteViewWidth, 0, 500);
                        invalidate();
                        state = 0;
                        return true;
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int scrollX = viewHolder.itemView.getScrollX();  //获得用户在滑动后，View相对初始位置移动的距离
                int dx = lastX - x; //得到用户实时移动的举例（横向）
                int dy = lastY - y;
                if (Math.abs(dx) > Math.abs(dy)) {  //只要左右移动的举例比上下移动的距离大，就执行滑动菜单操作
                    if (scrollX + dx >= deleteViewWidth) {    //检测右边界
                        viewHolder.itemView.scrollTo(deleteViewWidth, 0); //scrollTo()中的参数是指要“移动到的位置”
                        state = 1;
                        return true;    //表示已经消费这个事件，不必再传递了
                    } else if (scrollX + dx <= 0) {    //检测左边界
                        viewHolder.itemView.scrollTo(0, 0);
                        state = 0;
                        return true;
                    }
                    viewHolder.itemView.scrollBy(dx, 0);  //scrollBy()中的参数是指要“移动的距离（也就是像素的数量）”
                }
                break;
            case MotionEvent.ACTION_UP:
                int deltaX = 0;
                int upScrollX = viewHolder.itemView.getScrollX();//获得Item总共移动的距离
                if (upScrollX >= deleteViewWidth / 2) {  //如果显示超过一半，则弹性滑开
                    deltaX = deleteViewWidth - upScrollX;
                    state = 1;
                } else if (upScrollX < deleteViewWidth / 2) {//否则关闭
                    deltaX = -upScrollX;//在startScroll()方法中，第三个参数小于0，表示向右滑。
                    state = 0;
                }
                mScroller.startScroll(upScrollX, 0, deltaX, 0, 500);//弹性滑动
                invalidate();
                break;
        }
        lastX = x;
        lastY = y;
        return super.onTouchEvent(e);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                onTouchEvent(e);
                break;
            case MotionEvent.ACTION_MOVE:
                onTouchEvent(e);
                break;
            case MotionEvent.ACTION_UP:
                onTouchEvent(e);
                break;
        }
        return super.onInterceptTouchEvent(e);
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            viewHolder.itemView.scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            invalidate();
        }
    }
}
